<template>
  <div class="permission-container">
    <div class="app-container">
      <el-card shadow="always" :body-style="{ padding: '20px' }">
        <div slot="header">
          <el-row type="flex" justify="space-between" align="middle">
            <span>权限管理</span>
            <el-button type="primary" @click="openAdd(1, '0')">添加权限</el-button>
          </el-row>
        </div>
        <!-- card body -->
        <el-table border :data="list" row-key="id">
          <el-table-column prop="name" label="名称" />
          <el-table-column prop="code" label="标识" />
          <el-table-column prop="description" label="描述" />
          <el-table-column label="操作">
            <template #default="{ row }">
              <el-button v-if="row.type === 1" type="text" @click="openAdd(2, row.id)">添加</el-button>
              <el-button type="text" @click="openEdit(row.id)">编辑</el-button>
              <el-button type="text" @click="delPerm(row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
    </div>
    <!-- 添加权限弹出层 -->
    <el-dialog :visible="showDialog" title="弹层标题" @close="close">
      <el-form ref="fm" :rules="rules" label-width="100px" :model="formData">
        <el-form-item label="权限名称" prop="name">
          <el-input v-model="formData.name" />
        </el-form-item>
        <el-form-item label="权限标识" prop="code">
          <el-input v-model="formData.code" />
        </el-form-item>
        <el-form-item label="权限描述">
          <el-input v-model="formData.description" />
        </el-form-item>
        <el-form-item label="权限启用">
          <el-switch v-model="formData.enVisible" active-text="启用" active-value="1" inactive-text="不启用" inactive-value="0" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div style="text-align: right;">
          <el-button @click="showDialog = false">取消</el-button>
          <el-button type="primary" @click="addPerm">确定</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { getPermissionList, updatePermission, addPermission, delPermission, getPermissionDetail } from '@/api/permisson'
import { transformTreeData } from '@/utils'
export default {
  name: 'Permisssion',
  data () {
    return {
      showDialog: false,
      list: [],
      formData: {
        enVisible: '0', // 开启,1,开启，0，关闭
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        type: '', // 类型
        pid: '' // 添加到哪个节点下
      },
      rules: {
        name: [{ required: true, message: '权限点名称不能为空', trigger: 'blur' }],
        code: [{ required: true, message: '权限点标识不能为空', trigger: 'blur' }]
      }
    }
  },
  created () {
    this.getList()
  },
  methods: {
    openAdd (type, pid) {
      this.showDialog = true
      this.formData.type = type
      this.formData.pid = pid
    },
    // 打开编辑权限对话框
    async openEdit (id) {
      this.showDialog = true
      const detail = await getPermissionDetail(id)
      this.formData = detail
    },
    async addPerm () {
      try {
        await this.$refs.fm.validate()
        if (this.formData.id) {
          await updatePermission(this.formData)
        } else {
          await addPermission(this.formData)
        }
        this.getList()
        this.showDialog = false
        this.$message.success(`操作：${this.formData.type === 1 ? '页面权限' : '页面下按钮权限'}成功！`)
      } catch (error) {
        console.log(error)
      }
    },
    // 关闭弹层
    close () {
      this.formData = {
        enVisible: '0', // 开启,1,开启，0，关闭
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        type: '', // 类型
        pid: '' // 添加到哪个节点下
      }
      this.$refs.fm.resetFields()
      this.showDialog = false
    },
    async getList () {
      const res = await getPermissionList()
      // console.table(res)
      // 数据转成树状结构
      this.list = transformTreeData(res)
    },
    async delPerm (row) {
      try {
        await this.$confirm(`确定要删除:${row.name}吗？`)
        if (row.children && row.children.length > 0) {
          return this.$message.error('页面下还有功能权限，不能直接删除！')
        }
        await delPermission(row.id)

        this.$message.success('删除成功！')
        this.getList()
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
